#! /bin/sh
#
# telescope    This shell script takes care of starting and stopping
#	       the telescope daemon.
#
#
### BEGIN INIT INFO
# Provides: telescope
# Required-Start: $syslog $local_fs $network
# Required-Stop: $syslog $local_fs $network
# Default-Start:
# Default-Stop: 0 1 6
# Short-Description: telescope daemon
# Description: Radio Telescope Daemon for Linux
### END INIT INFO

# Source function libarary
. /etc/rc.d/init.d/functions

# telescope config
. /etc/telescope.conf

mkdir -p /var/run/telescope 2>/dev/null
chown $TELESCOPE_USER /var/run/telescope
restorecon /var/run/telescope 2>/dev/null

USRPD_NAME=telescope_usrpd
SQLD_NAME=telescope_sqld

TELESCOPE_PREFIX=@CMAKE_INSTALL_PREFIX@

USRPD_OPTIONS="--output=/var/run/telescope/fifo"

if [ -n "$SAMP_RATE" ]
then
USRPD_OPTIONS="$USRPD_OPTIONS -s$SAMP_RATE"
fi

if [ -n "$GAIN" ]
then
USRPD_OPTIONS="$USRPD_OPTIONS -g$GAIN"
fi

if [ -n "$RX_FREQ" ]
then
USRPD_OPTIONS="$USRPD_OPTIONS -r$RX_FREQ"
fi

if [ -n "$PERIOD" ]
then
USRPD_OPTIONS="$USRPD_OPTIONS -p$PERIOD"
fi

if [ $VERBOSE == "1" ] 
then
	USRPD_OPTIONS="$USRPD_OPTIONS -v"
fi

USRPD_OPTIONS="$USRPD_OPTIONS $USRP_ADDRESS"

SQLD_OPTIONS="--input=/var/run/telescope/fifo"

if [ -n "$CROSS_A" ]
then
	SQLD_OPTIONS="$SQLD_OPTIONS --cross_a=$CROSS_A"
fi

if [ -n "$CROSS_B" ]
then
	SQLD_OPTIONS="$SQLD_OPTIONS --cross_b=$CROSS_B"
fi

if [ -n "$AUTO1_A" ]
then
	SQLD_OPTIONS="$SQLD_OPTIONS --auto1_a=$AUTO1_A"
fi

if [ -n "$AUTO1_B" ]
then
	SQLD_OPTIONS="$SQLD_OPTIONS --auto1_b=$AUTO1_B"
fi

if [ -n "$AUTO2_A" ]
then
	SQLD_OPTIONS="$SQLD_OPTIONS --auto2_a=$AUTO2_A"
fi

if [ -n "$AUTO2_B" ]
then
	SQLD_OPTIONS="$SQLD_OPTIONS --auto2_b=$AUTO2_B"
fi

SQLD_OPTIONS="$SQLD_OPTIONS $DATABASE"


case "$1" in
    start)
       echo "Starting telescope:" 
       echo -n "$USRPD_NAME:"

       [ `id -u` = 0 ] || exit 4
       
       mkfifo /var/run/telescope/fifo 2>/dev/null
       chown $TELESCOPE_USER /var/run/telescope/fifo

       daemon --user=$TELESCOPE_USER $TELESCOPE_PREFIX/bin/$USRPD_NAME $USRPD_OPTIONS
       RETVAL=$?
       echo
       [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$USRPD_NAME
       
       echo -n "$SQLD_NAME:"
       
       daemon --user=$TELESCOPE_USER $TELESCOPE_PREFIX/bin/$SQLD_NAME $SQLD_OPTIONS
       RETVAL=$?
       echo
       [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$SQLD_NAME
       ;;
    stop)
       echo "Shutting down telescope:"
       echo -n "$USRPD_NAME:"
       killproc $USRPD_NAME
       RETVAL=$?
       echo
       [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$USRPD_NAME
       echo -n "$SQLD_NAME:"
       killproc $SQLD_NAME
       RETVAL=$?
       echo
       [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$SQLD_NAME
       
       rm /var/run/telescope/fifo 2>/dev/null
       ;;
    restart|force-reload)
       $0 stop
       $0 start
       ;;
    condrestart)
       echo "$0: condrestart not implemeted"
       exit 3
       ;;
    reload)
       echo "$0: reload not implemented"
       exit 3
       ;;
    status)
       status $USRPD_NAME
       status $SQLD_NAME
       ;;
    *)
       echo "Usage: $0 {start|stop|restart|status}"
       exit 1
       ;;
esac
exit 0
